home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / WASTE Object Handlers v1.0 ƒ / Handler Source / WE_hfs_Handler.c next >
Encoding:
Text File  |  1995-03-19  |  5.4 KB  |  218 lines  |  [TEXT/MPCC]

  1. // File Object Handler for the WASTE Text Engine
  2. // by Michael F. Kamprath, kamprath@earthlink.net
  3. //
  4. // v1.0, 12 March 1995
  5. //
  6.  
  7. #include <Icons.h>
  8. #include <Drag.h>
  9. #include <LowMem.h>
  10. #include "Waste.h"
  11.  
  12. #include "WE_hfs_Handler.h"
  13. #include "WASTE_Objects.h"
  14. #include "GetFileIcon.h"
  15. #include "SendFinderOpen.h"
  16.  
  17. // Local Functions
  18. static void        SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
  19.  
  20. //
  21. // New Object Handler for HFS Objects
  22. //
  23. pascal OSErr    HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
  24. {
  25. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  26.  
  27.     // First size the object as WASTE requires
  28.     SizeHFSObject( defaultObjectSize, theHFS);
  29.     
  30.     // Set the object refcon to 0.  This is for safety
  31.     // as later we use the refcon to store the icon handle
  32.     // for this object.
  33.     WESetObjectRefCon( objectRef, nil );
  34.     
  35.     return(noErr);
  36. }
  37.  
  38. //
  39. // Dispose Object handler for HFS Objects
  40. //
  41. pascal OSErr    HandleDisposeHFS(WEObjectReference objectRef )
  42. {
  43. Handle    theHFS = WEGetObjectDataHandle(objectRef);
  44. Handle    iconCache = (Handle)WEGetObjectRefCon( objectRef );
  45.  
  46.     // If the object has an icon handle loaded, dispose of it.
  47.     if (iconCache != nil)
  48.     {
  49.         DisposeIconSuite( iconCache, true );
  50.     }
  51.     
  52.     // Dispos of the HFS data
  53.     DisposHandle(theHFS);
  54.  
  55.     return(MemError());
  56. }
  57.  
  58. //
  59. // Draw Object Handler for HFS objects
  60. //
  61.  
  62. pascal OSErr    HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
  63. {
  64. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  65. Handle        iconCache = (Handle)WEGetObjectRefCon( objectRef );
  66. FontInfo    fInfo;
  67. GrafPtr        thePort;
  68. Rect        iconRect;
  69. OSErr        iErr;
  70. short        oldTextFont, oldTextSize, oldTextFace;
  71. short        sysTextFont = LMGetSysFontFam();
  72. short        sysTextSize = LMGetSysFontSize();
  73.  
  74.     // if the object's refcon did not contain an icon handle, load one.
  75.     if (iconCache == nil)
  76.     {
  77.         HLockHi( (Handle)theHFS );
  78.         if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
  79.             iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
  80.         else
  81.         {
  82.             iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
  83.             
  84. //            iErr = NewDesktopIconSuite( (*theHFS)->fileSpec.vRefNum, (*theHFS)->fileCreator, 
  85. //                                (*theHFS)->fileType, &iconCache);
  86.             if (iErr)
  87.                 iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
  88.         }    
  89.         HUnlock( (Handle)theHFS );
  90.         if (iErr)
  91.             return(iErr);
  92.         WESetObjectRefCon( objectRef, (long)iconCache );
  93.     }
  94.     
  95.     // Determine the icon's rectangle and plot it
  96.     SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
  97.                         (destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
  98.     iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
  99.                                             (IconTransformType)ttNone,    iconCache);
  100.                                             
  101.     // Draw the file's title.  First save old font info for port.
  102.     GetPort(&thePort);
  103.     oldTextFont = thePort->txFont;
  104.     oldTextSize = thePort->txSize;
  105.     oldTextFace = thePort->txFace;
  106.     
  107.     TextFont( sysTextFont );
  108.     TextSize( sysTextSize );
  109.     TextFace( 0 );
  110.     GetFontInfo(&fInfo);
  111.     
  112.     MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
  113.     DrawString( (*theHFS)->fileSpec.name );
  114.     
  115.     TextFont( oldTextFont );
  116.     TextSize( oldTextSize );
  117.     TextFace( oldTextFace );
  118.     
  119.     return( iErr );
  120. }
  121.  
  122. //
  123. // Click Handler for HFS Objects.  
  124. //        Will send the finder an open selection apple event when object is clicked.
  125. //
  126.  
  127. static long    oldHFSClickTime = nil;
  128.  
  129. pascal OSErr    HandleClickHFS(    Point hitPt, 
  130.                                     short modifiers, 
  131.                                     long clickTime, 
  132.                                     WEObjectReference objectRef)
  133. {
  134. HFSFlavor    **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
  135. OSErr        iErr = noErr;
  136.  
  137.  
  138.  
  139.     if (oldHFSClickTime + GetDblTime() > clickTime )
  140.     {
  141.         HLockHi( (Handle)theHFS );
  142.         iErr = SendFinderOpenAE(&(*theHFS)->fileSpec);
  143.         HUnlock( (Handle)theHFS );
  144.     }
  145.     
  146.     oldHFSClickTime = clickTime;
  147.     return( iErr );
  148. }
  149.  
  150. //
  151. // SizeHFSObject()
  152. //        Used to return the display size of an HFS object
  153. //
  154. static void    SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
  155. {
  156. FontInfo    fInfo;
  157. GrafPtr        thePort;
  158. short        oldTextFont, oldTextSize, oldTextFace;
  159. short        strWidth, strHeight;
  160. short        sysTextFont = LMGetSysFontFam();
  161. short        sysTextSize = LMGetSysFontSize();
  162.  
  163.     GetPort(&thePort);
  164.     oldTextFont = thePort->txFont;
  165.     oldTextSize = thePort->txSize;
  166.     oldTextFace = thePort->txFace;
  167.     
  168.     TextFont( sysTextFont );
  169.     TextSize( sysTextSize );
  170.     TextFace( 0 );
  171.     
  172.     strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
  173.     GetFontInfo(&fInfo);
  174.     strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
  175.     
  176.     TextFont( oldTextFont );
  177.     TextSize( oldTextSize );
  178.     TextFace( oldTextFace );
  179.  
  180.     objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
  181.     objectSize->v = 32 + strHeight + 1;    
  182. }
  183.  
  184. //
  185. // InsertFileRefFromFSSpec()
  186. //        Inserts a file object into a WASTE instance.
  187. //
  188.  
  189. OSErr    InsertFileRefFromFSSpec( FSSpec *theFile, WEHandle theWE )
  190. {
  191. FInfo        fndrInfo;
  192. HFSFlavor    **theHFS;
  193. Point        corner = {32,32};
  194. OSErr        iErr;
  195.     
  196.     theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
  197.     if (theHFS)
  198.     {
  199.         // First construct the HFSFlavor object from the FSSpec
  200.         iErr = FSpGetFInfo(theFile,&fndrInfo);
  201.         if (iErr)
  202.         {
  203.             DisposHandle( (Handle)theHFS );
  204.             return(iErr);
  205.         }
  206.         HLock( (Handle)theHFS );
  207.         (*theHFS)->fileType = fndrInfo.fdType;
  208.         (*theHFS)->fileCreator = fndrInfo.fdCreator;
  209.         (*theHFS)->fdFlags = fndrInfo.fdFlags;
  210.         (*theHFS)->fileSpec = (*theFile);
  211.         HUnlock( (Handle)theHFS );
  212.         
  213.         // Now insert it into the WEHandle
  214.         WEInsertObject( flavorTypeHFS, (Handle)theHFS, corner, theWE );
  215.     }
  216.     return(noErr);
  217. }
  218.